package com.chaoyong.higo.activity;

import java.io.UnsupportedEncodingException;
import java.text.SimpleDateFormat;

import org.apache.http.entity.StringEntity;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.chaoyong.higo.R;
import com.chaoyong.higo.base.BaseActivity;
import com.chaoyong.higo.net.OApi;
import com.chaoyong.higo.utils.PrefUtils;
import com.chaoyong.higo.utils.V;
import com.chaoyong.higo.utils.Values;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest.HttpMethod;
import com.nostra13.universalimageloader.core.ImageLoader;

import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.ImageView.ScaleType;

/**
 * 修改晒单
 * 
 * @author zk
 */
public class ModifyOrderActivity extends BaseActivity {

	private LinearLayout base_left_iv;// 左侧返回按钮
	private TextView base_title_tv;// 标题
	private TextView base_right_tv;// 标题右侧文字
	
	private SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd HH:mm:ss");

	/** 商品图片 */
	private ImageView show_goods_iv;
	/** 商品信息 */
	private TextView show_goods_tv;
	/** 商品价格 */
	private TextView show_price_tv;
	/** 揭晓时间 */
	private TextView show_konw_times;
	/** 想说啥*/
	private EditText question_input;
	/** 想说啥de title*/
	private EditText title_input;
	/** 确定*/
	private TextView show_make_sure;
	/** 上次分享时间*/
	private TextView end_time;
	
	private String face_img;
	
	private String img,title,price,time,expect,goods_id,et_title,et_content;
	private String id;
	
	private LinearLayout add_img_ll;
	private ImageView add_img;
	
	private String[] imgs;

	@Override
	public int layoutResID() {
		return R.layout.activity_to_show_rder;
	}

	@Override
	public void findView() {
		findTitleIds();
		
		show_goods_iv = V.f(this, R.id.show_goods_iv);
		show_goods_tv = V.f(this, R.id.show_goods_tv);
		show_price_tv = V.f(this, R.id.show_price_tv);
		show_konw_times = V.f(this, R.id.show_konw_times);
		question_input = V.f(this, R.id.question_input);
		show_make_sure = V.f(this, R.id.show_make_sure);
		title_input = V.f(this, R.id.title_input);
		end_time = V.f(this, R.id.end_time);

		add_img_ll = V.f(this, R.id.add_img_ll);
		add_img = V.f(this, R.id.add_img);
		
		end_time.setText("上次晒单时间：");
		/*intent.putExtra("goods_id", id);
				intent.putExtra("expect", mList.get(position).getExpect());
				intent.putExtra("img", mList.get(position).getCover_img().get(0));
				intent.putExtra("goods_name", mList.get(position).getGoods_name());
				intent.putExtra("goods_price", mList.get(position).getGoods_price());
				intent.putExtra("share_time", mList.get(position).getShare_time());
				intent.putExtra("share_title", mList.get(position).getShare_title());
				intent.putExtra("share_info", mList.get(position).getShare_title());*/
		id = getIntent().getStringExtra("id");
		img = getIntent().getStringExtra("img");
		title = getIntent().getStringExtra("goods_name");
		price = getIntent().getStringExtra("goods_price");
		time = getIntent().getStringExtra("share_time");
		goods_id = getIntent().getStringExtra("goods_id");
		expect = getIntent().getStringExtra("expect");
		et_title = getIntent().getStringExtra("share_title");
		face_img = getIntent().getStringExtra("face_img");
		et_content = getIntent().getStringExtra("share_info");
		
		if(TextUtils.isEmpty(face_img)){
			add_img_ll.setVisibility(View.GONE);
			add_img.setVisibility(View.GONE);
		}else{
			add_img.setVisibility(View.GONE);
			try {
				JSONArray ja = new JSONArray(face_img);
				imgs = new String[ja.length()];
				for (int i = 0; i < ja.length(); i++) {
					//imgs[i] = ja.getString(i).split("")[1];
					ImageView iv = new ImageView(this);
					LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(80, 80);
					lp.setMargins(5, 0, 0, 0);
					iv.setLayoutParams(lp);
					iv.setScaleType(ScaleType.FIT_XY);
					ImageLoader.getInstance().displayImage(ja.getString(i), iv);
					add_img_ll.addView(iv);
				}
			} catch (JSONException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
			
			
		}
	}

	private void findTitleIds() {
		base_left_iv = V.f(this, R.id.base_left_iv);
		base_title_tv = V.f(this, R.id.base_title_tv);
		base_right_tv = V.f(this, R.id.base_right_tv);

		base_title_tv.setText("我要晒单");
		base_right_tv.setVisibility(View.GONE);
	}

	@Override
	public void init() {
		base_left_iv.setOnClickListener(this);
		show_make_sure.setOnClickListener(this);
		
		ImageLoader.getInstance().displayImage(img, show_goods_iv);
		show_goods_tv.setText(title);
		show_price_tv.setText(price);
		show_konw_times.setText(time);
		
		title_input.setText(et_title);
		question_input.setText(et_content);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.base_left_iv:// title左侧返回
			this.finish();
			break;
		case R.id.show_make_sure:// 确定
			if(TextUtils.isEmpty(title_input.getText().toString())){
				showToast("请输入晒单标题");
				return ;
			}
			if(TextUtils.isEmpty(question_input.getText().toString())){
				showToast("请输入晒单内容");
				return ;
			}
			shaidan();
			break;
		}
	}

	private void shaidan() {
		// TODO Auto-generated method stub
		JSONObject json = new JSONObject();
		JSONObject info = new JSONObject();
		try {
			/*info.put("uid", PrefUtils.getString(this, "userId", ""));
			info.put("goods_id", goods_id);
			info.put("expect", expect);*/
			info.put("title", title_input.getText().toString());
			info.put("info", question_input.getText().toString());
			info.put("id", id);
			info.put("save", 1);
		} catch (JSONException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		try {
			json.put("info", info);
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		RequestParams params = new RequestParams();
		try {
			params.setBodyEntity(new StringEntity(json.toString(), "utf-8"));
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		HttpUtils utils = new HttpUtils();
		utils.send(HttpMethod.POST, OApi.appport_share, params, new RequestCallBack<String>() {

			@Override
			public void onFailure(HttpException arg0, String arg1) {
				// TODO Auto-generated method stub
				showToast(arg1);
			}

			@Override
			public void onSuccess(ResponseInfo<String> arg0) {
				// TODO Auto-generated method stub
				String result = arg0.result;
				try {
					JSONObject jso = new JSONObject(result);
					if(jso.getInt("status") == 1){
						finish();
					}
					showToast(jso.getString("data"));
				} catch (JSONException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		});
	}

}
